package com.suning.jiucai.utils;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 * @author 14111222
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class HttpUtils {

    private static HttpURLConnection getConncetion(String url) throws IOException  {
        URL uri = new URL(url);
        HttpURLConnection connection = (HttpURLConnection)uri.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        return connection;
    }
    
    public interface Operator<T> {
        T doOperator(HttpURLConnection connection) throws IOException;
    }
    
    public static <T> T execute(String url, Operator<T> action) throws IOException {
        HttpURLConnection connection =getConncetion(url);
        try {
            return action.doOperator(connection);
        } catch (IOException ex) {
            throw ex;
        } finally {
            connection.disconnect();
        }
    }
}
